﻿using PCTInterface.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utility;
using Utity;

namespace PCTInterface
{
    public partial class OtherInterface : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string result = string.Empty;
            Dictionary<string, object> formParams = null;
            string m = "";
            bool isError = false;
            try
            {
                formParams = Tools.ConvertParams();
                Response.ContentType = "application/json";
                Response.ContentEncoding = Encoding.UTF8;
                int _timeOut = -1;
                if (formParams.Keys.Contains("pto"))
                {
                    _timeOut = int.Parse(formParams["pto"].ToString());
                }

                if (_timeOut > 0)
                {
                    _timeOut=  _timeOut + 1;
                }
                else
                {
                    _timeOut = 3;
                    formParams["pto"] = _timeOut;
                }
                Server.ScriptTimeout = _timeOut ;

                OperationWeb oWeb = new OperationWeb();
                
                string tag = Page.RouteData.Values.ContainsKey("tag")? Page.RouteData.Values["tag"].ToString() :"";
       
                m = oWeb.ValidateAction(formParams);
                Dictionary<string, Func<string>> dicFunc = oWeb.InitMethods(formParams);
                if (dicFunc.ContainsKey(m))
                {
                    result = dicFunc[m]();
                }
                else
                {
                    result = dicFunc["default"]();
                }


                
            }
            catch(Exception ex)
            {
                isError = true;
                Logger.Instance.WriteEventLog("接口接收错误", "OtherInterface", formParams, ex);
                result = JSONHelper.formatReplyJsonNoResult(false,ex.Message,true) ;
                
            }

            if (m == "report_build")
            {
                Response.ContentType = "application/x-excel";
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.AddHeader("RETURN_CODE", isError ?"99"  : "0");
                Response.AddHeader("REQUEST_TOKEN", Tools.GenerateSign(formParams["REPORT_NAME"].ToString()));
                if (!isError)
                {
                    Response.TransmitFile(result);
                }
            }
            else {
                Response.ContentType = "application/json";
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.AddHeader("REQUEST_TOKEN", Tools.GenerateSign(result));
                result = AESSupport.AESEncryptIV(result);
                Response.Write(result);
            }


        }


    }
}